package com.kisszpy.core.dao;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.kisszpy.core.utils.PageModel;

public class HibernateGenericDao<T> extends GenericDao<T>{

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	public void save(T entity) {
		getSession().save(entity);
	}

	public void delete(T entity) {
		getSession().delete(entity);
	}

	public void update(T entity) {
		getSession().update(entity);
	}

	@SuppressWarnings("unchecked")
	public T findById(Class<T> className,Serializable id) {
		return (T) getSession().load(className, id);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll(Class<T> className) {
		Query query = getSession().createQuery("from "+className+" o");
		return query.list();
	}

	// 执行本地SQL
	public void executeNativeSQL(String sql,Object[] params) {
		SQLQuery sQuery = getSession().createSQLQuery(sql);
		if(params!=null){
			for(int i = 0;i<params.length;i++){
				sQuery.setParameter(i+1, params[i]);
			}
		}
		sQuery.executeUpdate();
	}

	public void executeNativeSQL(String sql) {
		this.executeNativeSQL(sql, null);
	}

	public void executeHQL(String hql, Object[] params) {
		Query query = getSession().createQuery(hql);
		if(params!=null){
			for(int i = 0;i<params.length;i++){
				query.setParameter(i, params[i]);
			}
		}
		query.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public List<T> executeQueryBySQL(String sql, Object[] params) {
		SQLQuery sQuery = getSession().createSQLQuery(sql);
		return sQuery.list();
	}

	@SuppressWarnings("unchecked")
	public List<T> executeQueryByHQL(String hql, Object[] params) {
		Query query = getSession().createQuery(hql);
		if(params!=null){
			for(int i = 0;i<params.length;i++){
				query.setParameter(i, params[i]);
			}
		}
		return query.list();
	}
	private void setParams(Query query,Object[] params){
		if(params!=null){
			for(int i = 0;i<params.length;i++){
				query.setParameter(i, params[i]);
			}
		}
	}
	// 生成排序操作
	public String orderBy(LinkedHashMap<String, String> map){
		if(map.keySet().size()>0){
			StringBuilder sb = new StringBuilder(" order by ");
			for(Iterator<String> it = map.keySet().iterator();it.hasNext();){
				String key = it.next();
				sb.append("o.").append(key).append(" ").append(map.get(key)).append(", ");
			}
			return sb.deleteCharAt(sb.lastIndexOf(",")).toString();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public PageModel<T> getAllEntity(Class<T> entityClassName,int pageNow,int pageSize,String condition,Object[] params,LinkedHashMap<String, String> orderBy){
		PageModel<T> pm = new PageModel<T>();
		Query query = getSession().createQuery("from "+entityClassName.getSimpleName()+" o "+(condition==null?"":" where "+condition)+" "+(this.orderBy(orderBy)==null?"":this.orderBy(orderBy)));
		query.setMaxResults(pageSize).setFirstResult((pageNow-1)*pageSize);
		this.setParams(query, params);
		pm.setItems(query.list());
		query = getSession().createQuery("select count(o) from "+entityClassName.getSimpleName()+" o "+(condition==null?"":" where "+condition)+"");
		this.setParams(query, params);
		String result = query.uniqueResult().toString();
		pm.setPageNow(pageNow);
		pm.setPageSize(pageSize);
		pm.setTotal(new Long(result));
		return pm;
	}
	

}
